/*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 *
 * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
 *
 * Contributor(s): 
 *  Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
 *  
 * If you didn't download this code from the following link, you should check
 * if you aren't using an obsolete version: http://www.isqlviewer.com
 */
package org.isqlviewer.ui.listeners;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.Point;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragGestureRecognizer;
import java.awt.dnd.DragSource;
import java.awt.dnd.DragSourceDragEvent;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DragSourceEvent;
import java.awt.dnd.DragSourceListener;
import java.awt.image.BufferedImage;

import javax.swing.JTree;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreePath;

import org.isqlviewer.bookmarks.BookmarkReference;
import org.isqlviewer.util.LoggableObject;

/**
 * Listener class for getting bookmark reference objects from the tree model for drag gestures.
 * <p>
 * 
 * @author Mark A. Kobold &lt;mkobold at isqlviewer dot com&gt;
 * @version 1.0
 */
public class BookmarkTreeDragListener extends LoggableObject implements DragGestureListener, DragSourceListener {

    private static final Point DRAG_OFFSET = new Point(2, 2);
    private JTree bookmarkTree = null;

    public BookmarkTreeDragListener(JTree treBookmarks) {

        this.bookmarkTree = treBookmarks;
    }

    public void dragGestureRecognized(DragGestureEvent event) {

        DragSource dndDragSource = DragSource.getDefaultDragSource();
        DragGestureRecognizer dndRecognizer = event.getSourceAsDragGestureRecognizer();
        bookmarkTree.cancelEditing();
        int x = event.getDragOrigin().x;
        int y = event.getDragOrigin().y;
        boolean leaf = true;
        int row = bookmarkTree.getRowForLocation(x, y);
        TreePath tp = bookmarkTree.getPathForLocation(x, y);
        if (tp == null) {
            dndRecognizer.resetRecognizer();
            return;
        }
        Object selected = tp.getLastPathComponent();
        if (selected == null) {
            dndRecognizer.resetRecognizer();
            return;
        }
        Transferable data = null;
        if (selected instanceof BookmarkReference) {
            data = (BookmarkReference) selected;
            leaf = true;
        } else {
            data = new StringSelection(selected.toString());
            leaf = false;
        }
        BufferedImage bi = null;
        if (DragSource.isDragImageSupported()) {
            GraphicsConfiguration gConfig = bookmarkTree.getGraphicsConfiguration();
            TreeCellRenderer tcr = bookmarkTree.getCellRenderer();
            Component comp = tcr.getTreeCellRendererComponent(bookmarkTree, selected, false, false, leaf, row, false);
            Dimension dim = comp.getPreferredSize();
            comp.setSize(dim);
            bi = gConfig.createCompatibleImage(dim.width, dim.height);
            Graphics2D g2 = bi.createGraphics();
            comp.paint(g2);
            g2.setColor(new Color(255, 255, 255, 112));
            g2.fillRect(0, 0, dim.width, dim.height);
        }
        try {
            dndDragSource.startDrag(event, null, bi, DRAG_OFFSET, data, this);
        } catch (Exception error) {
            dndRecognizer.resetRecognizer();
        }
    }

    public void dragDropEnd(DragSourceDropEvent dsde) {

    }

    public void dragEnter(DragSourceDragEvent dsde) {

    }

    public void dragExit(DragSourceEvent dse) {

    }

    public void dragOver(DragSourceDragEvent dsde) {

    }

    public void dropActionChanged(DragSourceDragEvent dsde) {

    }

}
